package com.elepant.tank;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Elephant
 * Date：2021/7/16.
 */
public class TankFrame extends Frame {
    public static int ScreenWidth = 800;
    public static int ScreenHeight = 800;

    //主战坦克
    Tank myTank = new Tank(200, 500, Direction.Up, Group.Good, this);

    //子弹列表
    List<Bullet> bulletList = new ArrayList<Bullet>();
    //Bullet bullet = new Bullet(300,200,Direction.Right);

    //敌军坦克列表
    public List<Tank> tanks = new ArrayList<>();

    //爆炸效果列表
    List<Explode> explodes = new ArrayList<>();


    public TankFrame() {
        setSize(ScreenWidth, ScreenHeight);
        setTitle("Tank war");
        setVisible(true);

        this.addKeyListener(new MykeyListener());

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    Image offScreenImage = null;

    @Override
    public void update(Graphics g) {
        if (offScreenImage == null) {
            offScreenImage = this.createImage(ScreenWidth, ScreenHeight);
        }
        Graphics gOffScreen = offScreenImage.getGraphics();
        Color c = gOffScreen.getColor();
        gOffScreen.setColor(Color.BLACK);
        gOffScreen.fillRect(0, 0, ScreenWidth, ScreenHeight);
        gOffScreen.setColor(c);
        paint(gOffScreen);
        g.drawImage(offScreenImage, 0, 0, null);
    }

    @Override
    public void paint(Graphics graphics) {
        Color c = graphics.getColor();
        graphics.setColor(Color.white);
        graphics.drawString("子弹数量：" + bulletList.size(), 15, 50);
        graphics.drawString("敌军数量：" + tanks.size(), 15, 65);
        graphics.drawString("爆炸数量：" + explodes.size(), 15, 85);
        graphics.setColor(c);

        //画出主战坦克
        myTank.paint(graphics);

        //画出所有子弹
        for (int b = 0; b < bulletList.size(); b++) {
            Bullet bullet = bulletList.get(b);
            bullet.paint(graphics);
        }

        //画出所有的敌军坦克
        for (int i = 0; i < tanks.size(); i++) {
            Tank tank = tanks.get(i);
            tank.paint(graphics);
        }

        //画出爆炸的效果
        for (int i = 0; i < explodes.size(); i++) {
            explodes.get(i).paint(graphics);
        }

        //循环碰撞检测，每个子弹都跟每辆坦克做一次碰撞检测，如果有交集则表示碰到了一起，则需要销毁子弹和敌军坦克
        for (int b = 0; b < bulletList.size(); b++) {
            for (int t = 0; t < tanks.size(); t++) {
                Bullet bullet = bulletList.get(b);
                Tank tank = tanks.get(t);
                bullet.colliedWith(tank);
            }
        }

        //边界检测（不要敌军坦克跑出边界）放在坦克类里面执行
        //boundaryDetection();

    }

    /*private void boundaryDetection() {
        for (int i = 0; i < tanks.size(); i++) {
            Tank enemyTank = tanks.get(i);
            if (enemyTank.getX() < 2) {
                enemyTank.setX(2);
            }
            if (enemyTank.getY() < 10) {
                enemyTank.setY(10);
            }
            if (enemyTank.getX() > ScreenWidth - Tank.WIDTH - 2) {
                enemyTank.setX(ScreenWidth - Tank.WIDTH - 2);
            }
            if (enemyTank.getY() > ScreenHeight - Tank.HEIGHT - 2) {
                enemyTank.setY(ScreenHeight - Tank.HEIGHT - 2);
            }
        }
    }*/

    class MykeyListener extends KeyAdapter {

        private boolean bL = false;
        private boolean bR = false;
        private boolean bU = false;
        private boolean bD = false;

        @Override
        public void keyPressed(KeyEvent e) {
            System.out.println("keyPressed");
            switch (e.getKeyCode()) {
                case KeyEvent.VK_DOWN:
                    bD = true;
                    break;
                case KeyEvent.VK_UP:
                    bU = true;
                    break;
                case KeyEvent.VK_LEFT:
                    bL = true;
                    break;
                case KeyEvent.VK_RIGHT:
                    bR = true;
                    break;
                default:
                    break;
            }
            setMainTankDirection();
        }


        @Override
        public void keyReleased(KeyEvent e) {
            System.out.println("keyReleased");
            switch (e.getKeyCode()) {
                case KeyEvent.VK_DOWN:
                    bD = false;
                    break;
                case KeyEvent.VK_UP:
                    bU = false;
                    break;
                case KeyEvent.VK_LEFT:
                    bL = false;
                    break;
                case KeyEvent.VK_RIGHT:
                    bR = false;
                    break;
                case KeyEvent.VK_SPACE:
                    myTank.fire();
                default:
                    break;
            }
            setMainTankDirection();
        }

        private void setMainTankDirection() {
            if (!bL && !bR && !bD && !bU) {
                myTank.setMoving(false);
            } else {
                myTank.setMoving(true);
                if (bU) myTank.setDirection(Direction.Up);
                if (bD) myTank.setDirection(Direction.Down);
                if (bR) myTank.setDirection(Direction.Right);
                if (bL) myTank.setDirection(Direction.Left);
            }
        }
    }


}
